package edu.pku.se.chocolate.UI.View;

import edu.pku.se.chocolate.ChocolateActivity;
import edu.pku.se.chocolate.R;
import edu.pku.se.chocolate.datastructure.Category;
import edu.pku.se.chocolate.datastructure.Tag;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AddpreDefTagView extends ChocolateView{
	
	protected Category AtCg;
	
	public AddpreDefTagView(View _v, Category _cg) // Category _cg;
	{
		super(_v);
		AtCg = _cg;		
//		TextView tv = (TextView)_v.findViewById(R.id.preToptv);
//		tv.setGravity(Gravity.CENTER);
//		tv.setTextAppearance(mainActivity, android.R.style.TextAppearance_Large);
	}
	
	public void preButtonListener(View v)
	{
		setButtonListener(v);
	}
	
	public void preInitial()
	{
		EditText kwet = (EditText)self.findViewById(R.id.preTagkwet);
		EditText tget = (EditText)self.findViewById(R.id.preTagNameet);
		kwet.setText("");
		tget.setText("");
	}
	
	private void setButtonListener(final View v)
	{
//		AtCg = _cg;	
//		Log.v("p1 ", "--");
		Button bt_no = (Button)v.findViewById(edu.pku.se.chocolate.R.id.preCancel);
//		Log.v("p2 ", "--");
		Button bt_yes = (Button)v.findViewById(edu.pku.se.chocolate.R.id.preConfirm);
//		Log.v("p3 ", "--");
//		Log.v("p3-1 ", "--");
		bt_no.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// back to message ui
				mainActivity.onBackPressed();
			}
			
		});
		
//		Log.v("p4 ", "--");
		
		bt_yes.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// get data
				
				int tag_id = 0;
				int msg_id = -1;
				int msg_num = 0;
				int type = 1;
//				String src = AtCg.name;
				
//				String catagoryName = AtCg.name;
				
				EditText kwV = (EditText)v.findViewById(edu.pku.se.chocolate.R.id.preTagkwet);
				EditText tgV = (EditText)v.findViewById(edu.pku.se.chocolate.R.id.preTagNameet);
				
				String kwStr = null;
				String tgStr = null;
				
				kwStr = kwV.getText().toString();
				tgStr = tgV.getText().toString();
				
				if(tgStr.equals(""))
				{
					tgStr = kwStr;
				}
				
				if(tgStr.equals(""))
				{
					Toast.makeText(ChocolateView.mainActivity, "关键字和标签名不能同时为空", Toast.LENGTH_SHORT).show();
					return;
				}
				
				kwV.setText("");
				tgV.setText("");
								
				CheckBox cb = (CheckBox)v.findViewById(edu.pku.se.chocolate.R.id.preTagremindcb);
				
				int isNotify = 0;
				if(cb.isChecked())			isNotify = 1;


				Tag tg = new Tag(tag_id, AtCg.getStorageName(), tgStr, AtCg.getStorageName(), kwStr, type, msg_num, msg_id, isNotify);
				ChocolateActivity.c_solver.addTag(tg);
//				boolean addRet = ChocolateActivity.c_solver.addTag(tg);
//				if(addRet)
//				{
//					Log.v("addRet", "ok");
//				}
//				else
//				{
//					Log.v("addRet", "no");
//				}
//				Log.v("kw", "kkk" + kwStr);
//				Log.v("tg", "ttt" + tgStr);
//				
//				Log.v("type", type + "");
//				Log.v("cb", isNotify + "");
				
				mainActivity.onBackPressed();
				
			}
			
		});
		
		
		TextView tv = (TextView)v.findViewById(R.id.preToptv);
		tv.setText("*添加标签*");
		tv.setGravity(Gravity.CENTER);
		tv.setTextAppearance(mainActivity, android.R.style.TextAppearance_Large);

//		Log.v("p5 ", "--");
	}
	
	public void show() {
		ChocolateActivity.c_solver.setautoReflash(null,null);
		super.show(); 
	}
}
